<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { counterClassify } from '@/pinia/getter';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
import { ElMessageBox } from 'element-plus';
  const formLabelWidth = '140px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    title:'',
    formTitle:'',
    dialogTableVisible:false,
    dialogFormVisible:false,
    disabled:false,
    list:[] as Array<any>,
  })
  interface formQuert {
    counter_id:number
    counter_cate_id:number
    name:string
    sort:number
    status:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    name: [{ required: true, trigger: "blur", message: "请输入标签名称" }],
    sort: [{ required: true, trigger: "blur", message: "请输入标签排序" }],
    status: [{ required: true, trigger: "blur", message: "请选择标签状态" }],
  }
  const handleForm = (row:any={})=>{
    const { counter_cate_id='', name='', sort='', status='' } = row
    action.formTitle = counter_cate_id?'编辑标签':'新增标签'
    Object.assign(form,{
      counter_cate_id,
      name,
      sort,
      status
    })
    action.dialogFormVisible = true
  }
  const handleDel = (row:any={})=>{
    const { counter_cate_id='', name='' } = row
    if(!counter_cate_id) return ToastWarning('缺少sku_id')
    ElMessageBox.confirm(`确定删除${name}标签吗？`, '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      const loading = Toastloading()
      propApi.delCounterCate({counter_cate_id}).then(()=>{
        ToastSuccess('删除成功')
        getList()
        loading.close()
      }).catch(()=>{
        loading.close()
      })
    }).catch(() => { })
  }
  const handleFormSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      const { counter_cate_id } = form
      propApi.addCounterCate(form).then(()=>{
        ToastSuccess(counter_cate_id?'编辑成功':'新增成功')
        closeFormDialog()
        $emit('updateList')
        getList()
        loading.close()
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const handleSerach = ()=>{
    action.list = []
    getList()
  }
  const getList = async()=>{
    if(!form.counter_id) return action.list = []
    const res = await propApi.getCounterCate({ counter_id:form.counter_id })
    const { data } = res
    action.list = data
  }
  const openDialog = async()=> {
    const loading = Toastloading()
    form.counter_id = 1
    await getList()
    loading.close()
    action.dialogTableVisible = true
  }
  const closeFormDialog = ()=>{
    form = reactive({
      counter_id:form.counter_id
    } as formQuert)

    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog xm-dialog-cont-y"
    v-model="action.dialogTableVisible"
    width="55%"
    title="货架标签">
      <div class="xm-headTab">
        <div class="labelBox">
          <div class="labelItem">
            <div class="label">货架类型：</div>
            <el-select v-model="form.counter_id" @change="getList" placeholder="请选择货架类型">
              <el-option
                v-for="item in counterClassify"
                :label="item.label"
                :value="item.type"
              />
            </el-select>
          </div>
          <div class="labelItem">
            <el-button type="primary" @click="handleSerach">查询</el-button>
            <el-button type="primary" @click="handleForm">新增</el-button>
          </div>
        </div>
      </div>
      <el-table
        class="xm-main xm-TableCenter"
        :data="action.list"
        border
      >
        <el-table-column min-width="160" prop="name" label="标签名称" />
        <el-table-column min-width="100" prop="status" label="标签状态">
          <template #default="scoped">
            {{ ['隐藏','显示'][scoped.row.status] }}
          </template>
        </el-table-column>
        <el-table-column min-width="160" prop="created_at" label="创建时间" />
        <el-table-column sortable min-width="100" prop="sort" label="标签排序" />
        <el-table-column width="120" label="操作">
          <template #default="scoped">
            <el-button link type="primary" @click="handleForm(scoped.row)">编辑</el-button>
            <el-button link type="danger" @click="handleDel(scoped.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="35%"
      :title="action.formTitle">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="标签名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入标签名称" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="标签排序" prop="sort">
          <el-input v-model="form.sort" @input="allNumber(form,'sort')" placeholder="请输入标签排序" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="标签状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择标签状态">
            <el-option label="隐藏" :value="0" />
            <el-option label="显示" :value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeFormDialog">取 消</el-button>
          <el-button type="primary" @click="handleFormSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>